package com.accesshollywood.grfxOrdering;

import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.GraphicsEnvironment;
import java.util.Arrays;
import java.util.List;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.accesshollywood.SQL.GFXSQL;
import com.accesshollywood.components.FoldablePanel;
import com.accesshollywood.email.Email;
import com.accesshollywood.grfxOrdering.Dialogs.ProgressBarDialog;
import com.accesshollywood.grfxOrdering.basics.Destinations;
import com.accesshollywood.grfxOrdering.basics.GraphicTypes;
import com.accesshollywood.grfxOrdering.basics.PreApprovedDictionary;
import com.accesshollywood.grfxOrdering.basics.ShowNames;
import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.grfxOrdering.panels.ViewFilterGroupPanel;
import com.accesshollywood.grfxOrdering.panels.ViewFiltersMasterPanel;
import com.accesshollywood.notifications.ChimeAlert;
import com.accesshollywood.p2p.P2P;
import com.accesshollywood.prefs.Prefs;
import com.accesshollywood.speech.Speak;
import com.accesshollywood.viewFiltering.ViewFilters;

public class GRFXOrdersMain {
	private static final String			LOOKANDFEEL	= "System";
	// private static final String LOOKANDFEEL = "Metal";
	// private static final String LOOKANDFEEL = "Motif";
	// private static final String LOOKANDFEEL = "GTK";

	public static OrderMainGUI			gui;

	public static P2P					p2p;

	public static PreApprovedDictionary	preApprovedDictionary;

	public static ProgressBarDialog		pBarDlg		= new ProgressBarDialog(GRFXOrdersMain.gui, Dialog.ModalityType.MODELESS);

	public static void main(String[] args) {

		Runtime.getRuntime().addShutdownHook(new Thread() {
			// This method is called during shutdown
			public void run() {
				Prefs.save();
				GFXSQL.unregisterConnection();
			}
		});

		setupUISkin();

		Prefs.load();
		ChimeAlert.setup();
		Speak.setup();
		Email.setup();

		createSQLConnection();
		Users.populateUsers();
		GFXSQL.registerConnection();

		if (Users.getCurrentUser()) {
			ShowNames.populateShowNames();
			GraphicTypes.populateGraphicTypes();
			Destinations.populateDestinations();
			ViewFilters.populateViewFilters();
			ViewFilters.populateViewFilterPresets();
		}

		p2p = new P2P();

		preApprovedDictionary = new PreApprovedDictionary();

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				if (Users.getCurrentUser()) {
					createAndShowGUI();
				} else {
					Prefs.save();
					GFXSQL.unregisterConnection();
					System.exit(0);
				}
			}
		});
	}

	private static void createSQLConnection() {
		try {
			GFXSQL.connectDataBase("AH_gfx_orders", "AH_gfx_ordersARCHIVE");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void createAndShowGUI() {
		setGUI(new OrderMainGUI());
		// gui.pack();
		refresh(true);
		// gui.resetFrame();
	}

	public static void foldupPresetPanel() {
		for (FoldablePanel currFoldable : gui.allFoldables) {
			if (currFoldable instanceof ViewFilterGroupPanel) {
//				currFoldable.doFoldUnfold(FoldablePanel.FOLD_SPEED.FAST);
			}
		}
	}

	public static void refresh(boolean fromSQL) {
		gui.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		OrderMainGUI.refresh(fromSQL);
		gui.repaint();
		// OrderMainGUI.setFoldablesMaxHeight();
		gui.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}

	public static void refresh(String fieldName, int[] idOrders, boolean fromSQL) {
		OrderMainGUI.refresh(fieldName, idOrders, fromSQL);
		// if (fromSQL) {
		// P2P.refreshPeers(fieldName, idOrders);
		// }
	}

	public static void refresh(int[] idOrders, boolean fromSQL) {
		OrderMainGUI.refresh(idOrders, fromSQL);
		// if (fromSQL) {
		// P2P.refreshPeers(idOrders);
		// }
	}

	public static void refreshSolo(String fieldName, int[] orderNumbers, boolean fromSQL) {
		OrderMainGUI.refresh(fieldName, orderNumbers, fromSQL);
	}

	public static void refreshSolo(int[] orderNumbers, boolean fromSQL) {
		OrderMainGUI.refresh(orderNumbers, fromSQL);
	}

	public static void refreshSolo(boolean fromSQL) {
		OrderMainGUI.refresh(fromSQL);
	}

	public static void refresh(int[] orderNumbers) {
		if (ViewFiltersMasterPanel.forceRefresh()) {
			OrderMainGUI.refresh(orderNumbers, true);
		}
		OrderMainGUI.setFoldablesMaxHeight();
	}

	public static void refreshRequest(String fieldName, int[] orderNumbers, boolean passToPeers) {
		if (passToPeers) {
			if (fieldName.equals("NULL")) {
				if (orderNumbers != null) {
					refresh(orderNumbers, true);
				} else {
					refresh(true);
				}
			} else {
				if (orderNumbers != null) {
					refresh(fieldName, orderNumbers, true);
				} else {
					refresh(true);
				}
			}
		} else {
			if (fieldName.equals("NULL")) {
				if (orderNumbers != null) {
					refreshSolo(orderNumbers, true);
				} else {
					refreshSolo(true);
				}
			} else {
				if (orderNumbers != null) {
					refreshSolo(fieldName, orderNumbers, true);
				} else {
					refreshSolo(true);
				}
			}
		}
	}

	public static OrderMainGUI getGUI() {
		return gui;
	}

	public static void setGUI(OrderMainGUI GUI) {
		gui = GUI;
	}

	public static void setupUISkin() {
		String lookAndFeel = null;

		if (LOOKANDFEEL != null) {
			if (LOOKANDFEEL.equals("Metal")) {
				lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
				// an alternative way to set the Metal L&F is to replace the
				// previous line with:
				// lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";

			}

			else if (LOOKANDFEEL.equals("System")) {
				lookAndFeel = UIManager.getSystemLookAndFeelClassName();
			}

			else if (LOOKANDFEEL.equals("Motif")) {
				lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
			}

			else if (LOOKANDFEEL.equals("GTK")) {
				lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
			}

			else {
				System.err
						.println("Unexpected value of LOOKANDFEEL specified: "
								+ LOOKANDFEEL);
				lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
			}

			try {

				UIManager.setLookAndFeel(lookAndFeel);

			}

			catch (ClassNotFoundException e) {
				System.err
						.println("Couldn't find class for specified look and feel:"
								+ lookAndFeel);
				System.err
						.println("Did you include the L&F library in the class path?");
				System.err.println("Using the default look and feel.");
			}

			catch (UnsupportedLookAndFeelException e) {
				System.err.println("Can't use the specified look and feel ("
						+ lookAndFeel + ") on this platform.");
				System.err.println("Using the default look and feel.");
			}

			catch (Exception e) {
				System.err.println("Couldn't get specified look and feel ("
						+ lookAndFeel + "), for some reason.");
				System.err.println("Using the default look and feel.");
				e.printStackTrace();
			}
			List<String> fonts = Arrays.asList(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
			// UIManager.put("Table.font", new Font(fonts.get(fonts.indexOf("Arial")), Font.PLAIN, 24));
		}
	}
}